package com.server.server_a10.management.controller;

import com.server.server_a10.entity.Inform;
import com.server.server_a10.entity.User;
import com.server.server_a10.management.entity.ManageInf;
import com.server.server_a10.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class manageUserHandle {
    @Autowired
    UserMapper userMapper;

    @GetMapping(value = "/adminGetUser")
    public ManageInf adminShowUser(@RequestParam("limit") int limit,
                                   @RequestParam("page") int page,
                                   @RequestParam("name") Object n) {
        ManageInf inform = new ManageInf();

        String name = (String) n;
        System.out.println(limit + page + name);

        int index = (page - 1) * limit;

        List<User> users;
        //直接分页
        if (name.isEmpty() || name.equals("\"\"")) {
            users = userMapper.getUsersLimit(index, limit);
        } else {
            //模糊查询后分页
            users = userMapper.getUserNameLimit(index, limit, name);
        }
        int count = userMapper.getUserCount();
        inform.setCount(count);
        if (users.isEmpty()) {
            inform.isNull("似乎没有任何用户数据");
        } else {
            inform.success("用户信息加载成功",users);
        }

        return inform;
    }

    @PostMapping(value = "/adminUpdateUser")
    public Inform adminUpdateUser(@RequestBody User user) {
        Inform inform = new Inform();

        int isOk = userMapper.updateById(user);
        if (isOk == 0) {
            inform.fail("修改失败，请检查数据格式",null);
        } else {
            inform.success("用户信息修改成功",null);
        }
        return inform;
    }

    @PostMapping(value = "/adminInsUser")
    public Inform adminInsUser(@RequestBody User user) {
        Inform inform = new Inform();

        System.out.println(user.toString());
        boolean idNull = user.getId()==0;
        boolean nameNull = user.getName().isEmpty();
        boolean sexNull = user.getSex().isEmpty();
        boolean addressNull = user.getAddress().isEmpty();
        boolean phoneNull = user.getPhone().isEmpty();
        boolean passwdNull = user.getPasswd().isEmpty();

        if (idNull||nameNull||sexNull||addressNull||phoneNull||passwdNull){
            inform.fail("请输入完整数据!!",null);
            return inform;
        }

        int isOk = userMapper.insert(user);
        if (isOk == 0) {
            inform.fail("添加失败，请检查数据格式",null);
        } else {
            inform.success("用户添加成功",null);
        }
        return inform;
    }

    @PostMapping(value = "/adminDelUser")
    public Inform adminDelUser(@RequestBody Map<String, Object> request) {
        Inform inform = new Inform();

        int id = (int) request.get("id");

        int isOk = userMapper.deleteById(id);
        if (isOk == 0) {
            inform.setCode(100);
            inform.setMsg("删除失败，请检查数据格式");
        } else {
            inform.setCode(200);
            inform.setMsg("用户删除成功");
        }
        return inform;
    }

    @PostMapping(value = "/adminDelUsersByList")
    public Inform adminDelUsersByList(@RequestBody List<Integer> usersId) {
        Inform inform = new Inform();

        int isOk = userMapper.deleteBatchIds(usersId);
        if (isOk == 0) {
            inform.setCode(100);
            inform.setMsg("删除失败，请检查数据格式");
        } else {
            inform.setCode(200);
            inform.setMsg("用户删除成功");
        }
        return inform;
    }
}
